//
//  RoomData.m
//  Model
//
//  Created by YDJH on 2017/5/19.
//  Copyright © 2017年 YDJH. All rights reserved.
//
#define skRandom(from,to) (int)(from + (arc4random() % (to - from + 1))) //生成随机数[from,to]

#import "RoomData.h"
#import "BuildingData.h"
#import "HZWallData.h"
#import "HZFurniture_Header.h"

@implementation RoomData

+ (NSMutableArray *)gobackRoomModel {
    
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"RoomData" ofType:@"plist"];
    NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath];
    NSMutableArray *mutableArr = [NSMutableArray array];
    for (NSDictionary *dict in array) {
        RoomData *model =[RoomData new];
        model.name = dict[@"RoomName"];
        model.type = dict[@"RoomType"];
        model.roomCategory = dict[@"RoomCategory"];
        model.insideRoomFurniture = dict[@"insideFurniture"];
        model.outsideRoomFurniture = dict[@"outsideFurniture"];
        model.insideRightDoorPosition = dict[@"insideRightDoorPosition"];
        model.insideRightWindowPosition = dict[@"insideRightWindowPosition"];
        model.insidePicture = dict[@"insidePicture"];
        model.insideLadderPosition1 = dict[@"insideLadderPosition1"];
        model.insideLadderPosition2 = dict[@"insideLadderPosition2"];
        model.outsideLadderPosition = dict[@"outsideLadderPosition"];
        model.insideBackDoorPosition = dict[@"insideBackDoorPosition"];
        
        [mutableArr addObject:model];
    }
    return mutableArr;
}

+ (HZRoom *)gobackRoomModelWithModel:(RoomData *)room {
    HZRoom *model = [[HZRoom alloc] init];
    model.name = room.name;
    model.type = room.type;
    model.roomCategory = room.roomCategory;
    
    return model;
}


+ (NSMutableArray *)gobackCurrentRoomArr:(NSArray *)insideRoomType withRoomArr:(NSArray *)roomArr{
    
    NSMutableArray *currentRoomArr = [NSMutableArray array];
    for (NSString *type in insideRoomType) {
        for (RoomData *roomData in roomArr) {
            if ([roomData.type isEqualToString:type]) {
                [currentRoomArr addObject:roomData];
                break;
            }
        }
    }
    return currentRoomArr;
}

+ (NSMutableArray *)gobackCurrentLobbyArr:(NSArray *)buildingLobby withRoomArr:(NSArray *)roomArr {
    
    NSMutableArray *currentLobbyArr = [NSMutableArray array];
    for (NSString *type in buildingLobby) {
        for (RoomData *roomData in roomArr) {
            if ([roomData.type isEqualToString:type]) {
                [currentLobbyArr addObject:roomData];
                break;
            }
        }
    }
    currentLobbyArr = (NSMutableArray *)[[currentLobbyArr reverseObjectEnumerator] allObjects];
    return currentLobbyArr;
}
// 返回房间的楼房间的模型
+ (NSMutableArray *)gobackRoomArrWithBuildingData:(BuildingData *)buildingData withRoomArr:(NSArray *)roomArr{
    NSMutableArray *array = [NSMutableArray array];
    
    NSArray *structure =  [buildingData.structure componentsSeparatedByString:@","];
    NSString *floorNum = structure[0];
    NSString *roomNum = structure[1];
    //房间
    NSMutableArray *currentRoomArr = [self gobackCurrentRoomArr:buildingData.insideRoomType withRoomArr:roomArr];
    //大厅
    NSMutableArray *currentLobbyArr = [self gobackCurrentLobbyArr:buildingData.buildLobby withRoomArr:roomArr];
    
    for (int i = 0; i<floorNum.integerValue; i++) {
        NSMutableArray *floorArr = [NSMutableArray array];
        for (int j = 0; j<roomNum.integerValue; j++) {
            HZRoom *roomModel = [[HZRoom alloc] init];
            //初始化房间状态
            roomModel.roomState = RoomState_NeverCome;
            //房间在二维数组位置
            roomModel.position = [NSIndexPath indexPathForRow:j inSection:i];
            //获取当前房间下一层对应的房间
            HZRoom *bottomRoom = nil;
            if (i != 0) { //不是第一层
                if (j == 0 || j == roomNum.integerValue - 1) {
                    HZRoom *tempRoom = array[i-1][0];
                    if ([tempRoom.roomCategory isEqualToString:@"1"]) { //是大厅
                        bottomRoom = tempRoom;
                    }else {
                        bottomRoom = array[i-1][j];
                    }
                    
                }
            }
            
            if (i<currentLobbyArr.count) {              //初始化大厅,地下室
                RoomData *roomData = currentLobbyArr[i];
                roomModel.type = roomData.type;
                roomModel.name = roomData.name;
                roomModel.roomCategory = roomData.roomCategory;
                roomModel.roomData = roomData;
                
                //大厅中确定谁将会成为第一层
                if (i == 0) {
                    if ([roomData.type isEqualToString:Underground_Parking] || [roomData.type isEqualToString:Basement_Type]) {
                        roomModel.isFirstSurfaceRoom = NO;
                    }else {
                        roomModel.isFirstSurfaceRoom = YES;
                    }
                }else if (i == 1){
                    
                    if (bottomRoom.isFirstSurfaceRoom) {
                        roomModel.isFirstSurfaceRoom = NO;
                    }else {
                        roomModel.isFirstSurfaceRoom = YES;
                    }
                }else {
                    roomModel.isFirstSurfaceRoom = NO;
                }
                
                if (![roomData.type isEqualToString:Underground_Parking] && ![roomData.type isEqualToString:Basement_Type]) { //不是地下室 不是地下停车场
                    
                    //初始化外墙
                    roomModel.outsideWall1 = [HZWallData goBackOutsideWallWithLobbyRoom:roomModel indexPath:[NSIndexPath indexPathForRow:j inSection:i] maxsection:floorNum.integerValue maxrow:roomNum.integerValue picture:buildingData.outsideBackWallPicture bottomRoom:bottomRoom]; //后墙
                    roomModel.outsideWall2 = [HZWallData goBackOutsideWallWithMonochromaticWallPicture:buildingData.outsideLeftWallPicture]; //单色左墙
                    roomModel.outsideWall4 = [HZWallData goBackOutsideWallWithMonochromaticWallPicture:buildingData.outsideRightWallPicture]; //单色右墙
                    if (roomModel.isFirstSurfaceRoom) { //第一层要初始化主外墙
                        roomModel.outsideWall3 = [HZWallData goBackOutsideWallWithMainEntranceWallPicture:buildingData.outsideMainWallPicture furnitureArr:buildingData.outsideMainWallFurniture indexPath:[NSIndexPath indexPathForRow:j inSection:i]]; //主墙
                    }
                    
                }
                
                roomModel.insidewall1 = [HZWallData goBackInsideWallWithLobbyRoom:roomModel outsideWall:roomModel.outsideWall1 indexPath:[NSIndexPath indexPathForRow:j inSection:i] maxsection:floorNum.integerValue bottomRoom:bottomRoom];
                
                
                [floorArr addObject:roomModel];
                break;
            }else {
                if (j == 0) {       //每层第一个房间 是楼梯间
                    RoomData *staircaseData = [self gobackStaircaseRoomDataWithRoomDataArr:roomArr];
                    roomModel.type = staircaseData.type;
                    roomModel.name = staircaseData.name;
                    roomModel.roomCategory = staircaseData.roomCategory;
                    roomModel.roomData = staircaseData;
                    
                    //staircaseModel.roomState = staircaseData.roomState;
                    if (i==0) {
                        roomModel.isFirstSurfaceRoom = YES;
                    }else {
                        roomModel.isFirstSurfaceRoom = NO;
                    }
                    
                    
                    //初始化外墙
                    roomModel.outsideWall1 = [HZWallData goBackOutsideWallWithRoom:roomModel indexPath:[NSIndexPath indexPathForRow:j inSection:i] maxsection:floorNum.integerValue maxrow:roomNum.integerValue picture:buildingData.outsideBackWallPicture bottomRoom:bottomRoom]; //后墙
                    roomModel.outsideWall2 = [HZWallData goBackOutsideWallWithMonochromaticWallPicture:buildingData.outsideLeftWallPicture]; //单色左墙
                    
                    if (roomModel.isFirstSurfaceRoom) { //如果是楼梯间 第一层要初始化主外墙 右外墙
                        roomModel.outsideWall3 = [HZWallData goBackOutsideWallWithMainEntranceWallPicture:buildingData.outsideMainWallPicture furnitureArr:buildingData.outsideMainWallFurniture indexPath:[NSIndexPath indexPathForRow:j inSection:i]]; //主墙
                        roomModel.outsideWall4 = [HZWallData goBackOutsideWallWithMonochromaticWallPicture:buildingData.outsideRightWallPicture]; //单色右墙
                    }
                    roomModel.insidewall1 = [HZWallData goBackInsideWallWithRoom:roomModel  lastRoom:[floorArr lastObject] outsideWall:roomModel.outsideWall1 indexPath:[NSIndexPath indexPathForRow:j inSection:i] maxsection:floorNum.integerValue maxrow:roomNum.integerValue bottomRoom:bottomRoom];
                    
                    
                    [floorArr addObject:roomModel];
                    
                }else if (j == roomNum.integerValue -1) { //每层最后一间房
                    
                    if (j >= 3) {   //每层最后一个房间 大于3也是楼梯间
                        
                        RoomData *staircaseData = [self gobackStaircaseRoomDataWithRoomDataArr:roomArr];
                        roomModel.type = staircaseData.type;
                        roomModel.name = staircaseData.name;
                        roomModel.roomCategory = staircaseData.roomCategory;
                        roomModel.roomData = staircaseData;
                        //staircaseModel.roomState = staircaseData.roomState;
                        if (i==0) {
                            roomModel.isFirstSurfaceRoom = YES;
                        }else {
                            roomModel.isFirstSurfaceRoom = NO;
                        }
                        //初始化外墙
                        roomModel.outsideWall1 = [HZWallData goBackOutsideWallWithRoom:roomModel indexPath:[NSIndexPath indexPathForRow:j inSection:i] maxsection:floorNum.integerValue maxrow:roomNum.integerValue picture:buildingData.outsideBackWallPicture bottomRoom:bottomRoom]; //后墙
                        roomModel.outsideWall4 = [HZWallData goBackOutsideWallWithMonochromaticWallPicture:buildingData.outsideRightWallPicture]; //单色右墙
                        
                        
                        roomModel.insidewall1 = [HZWallData goBackInsideWallWithRoom:roomModel lastRoom:[floorArr lastObject] outsideWall:roomModel.outsideWall1 indexPath:[NSIndexPath indexPathForRow:j inSection:i] maxsection:floorNum.integerValue maxrow:roomNum.integerValue bottomRoom:bottomRoom];
                        
                        [floorArr addObject:roomModel];
                        
                    }else {
                        
                        RoomData *roomData = currentRoomArr[skRandom(0,currentRoomArr.count-1)];
                        roomModel.type = roomData.type;
                        roomModel.name = roomData.name;
                        roomModel.roomCategory = roomData.roomCategory;
                        roomModel.roomData = roomData;
                        
                        //roomModel.roomState = roomData.roomState;
                        if (i==0) {
                            roomModel.isFirstSurfaceRoom = YES;
                        }else {
                            roomModel.isFirstSurfaceRoom = NO;
                        }
                        
                        
                        //初始化外墙
                        roomModel.outsideWall1 = [HZWallData goBackOutsideWallWithRoom:roomModel indexPath:[NSIndexPath indexPathForRow:j inSection:i] maxsection:floorNum.integerValue maxrow:roomNum.integerValue picture:buildingData.outsideBackWallPicture bottomRoom:bottomRoom]; //后墙
                        roomModel.outsideWall4 = [HZWallData goBackOutsideWallWithMonochromaticWallPicture:buildingData.outsideRightWallPicture]; //单色右墙
                        
                        roomModel.insidewall1 = [HZWallData goBackInsideWallWithRoom:roomModel lastRoom:[floorArr lastObject] outsideWall:roomModel.outsideWall1 indexPath:[NSIndexPath indexPathForRow:j inSection:i] maxsection:floorNum.integerValue maxrow:roomNum.integerValue bottomRoom:bottomRoom];
                        
                        [floorArr addObject:roomModel];
                        
                    }
                    
                }else {//每层除了第一个 和最后一个其它的房间
                    
                    RoomData *roomData = currentRoomArr[skRandom(0,currentRoomArr.count-1)];
                    roomModel.type = roomData.type;
                    roomModel.name = roomData.name;
                    roomModel.roomCategory = roomData.roomCategory;
                    roomModel.roomData = roomData;
                    
                    //roomModel.roomState = roomData.roomState;
                    if (i==0) {
                        roomModel.isFirstSurfaceRoom = YES;
                    }else {
                        roomModel.isFirstSurfaceRoom = NO;
                    }
                    
                    
                    //初始化外墙
                    roomModel.outsideWall1 = [HZWallData goBackOutsideWallWithRoom:roomModel indexPath:[NSIndexPath indexPathForRow:j inSection:i]  maxsection:floorNum.integerValue maxrow:roomNum.integerValue picture:buildingData.outsideBackWallPicture bottomRoom:bottomRoom]; //后墙
                    
                    
                    
                    roomModel.insidewall1 = [HZWallData goBackInsideWallWithRoom:roomModel  lastRoom:[floorArr lastObject] outsideWall:roomModel.outsideWall1 indexPath:[NSIndexPath indexPathForRow:j inSection:i] maxsection:floorNum.integerValue maxrow:roomNum.integerValue bottomRoom:bottomRoom];
                    
                    [floorArr addObject:roomModel];
                }
            }
        }
        [array addObject:floorArr];
    }
    return array;
}

//楼梯间数据
+ (RoomData *)gobackStaircaseRoomDataWithRoomDataArr:(NSArray *)array {
    RoomData *data;
    for (RoomData *roomData in array) {
        if ([roomData.type isEqualToString:Stair_Room]) {
            data = roomData;
        }
    }
    return data;
}
@end
